/**
 * 
 */
package testy;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CIIdata;




/**
 * @author pdrabik
 *
 */
public class Compiler {
	
	private static final Logger log = LoggerFactory.getLogger(Compiler.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Compiler cmp = new Compiler();
		cmp.printCP();
		try {
			ClassPathHacker.addFile("D:\\prace\\workspace\\CII_1edit\\bin\\_main");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("", e);
		}
		log.debug("new\n\n\n");
		ClassLoader cls = ClassLoader.getSystemClassLoader();
		try {
			Class<?> c= cls.loadClass("_main.CIIMonitor");
			c.newInstance();
			Method mthl[] = c.getDeclaredMethods();
			for (int m=0;m<mthl.length;m++) {
				log.debug("met : " + mthl[m].getName());
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			log.error("", e);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			log.error("", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("", e);
		}
//		cmp.again();
		cmp.printCP();
		
	}
	
	public void printCP () {
		ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

        //Get the URLs
        URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

        for(int i=0; i< urls.length; i++)
        {
            log.debug(urls[i].getFile());
        } 
	}
	
	public void again() {
		ArrayList<Class> classList = new ArrayList<Class>(); 
        File dir = new File ("D:\\prace\\workspace\\CII_1edit\\bin\\_main"); 
        try { 
            if (dir.exists()) { 
                ArrayList<File> paths = new ArrayList<File>(); 
                ArrayList<String> classes = new ArrayList<String>(); 
                 
     
                 
                for (File f : dir.listFiles()) if (f.isDirectory()) paths.add(f); 
                paths.add(dir); 
                 
                for (File f : paths) 
                    for (File ff : f.listFiles()) 
                        if (!ff.isDirectory() && ff.getName().endsWith(".class")) 
                            classes.add((f != dir? f.getName() + "." : "") + ff.getName().substring(0, ff.getName().length() - 6)); 
                     
                 
                ClassLoader cl = new URLClassLoader(new URL [] {dir.toURI().toURL()}); 
                for (String s : classes) 
                    try { 
                    	s = "_main."+s;
                    	Class<?> tmp;
                        classList.add(tmp=cl.loadClass(s)); 
                        Method mthl[] = tmp.getDeclaredMethods();
            			for (int m=0;m<mthl.length;m++) {
            				log.debug(s+" :met: " + mthl[m].getName());
            			}
                    } catch (Exception e) {log.error("", e);} 
            } 
        } catch (IOException e) {}  
	}
}
